package com.jci.vsd.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jci.vsd.R;
import com.jci.vsd.bean.PicBean;

import java.util.List;

/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class PicFeedBackAdapter extends BaseAdapter {
    private Context context;
    private List<PicBean> mList;
    private LayoutInflater layoutInflater;
    private int maxCount =3; // 最多3张
    AddAndDeleteListener listener;
    public PicFeedBackAdapter(Context context, List<PicBean> list) {
        this.context = context;
        this.mList = list;
        layoutInflater = LayoutInflater.from(this.context);
    }


    public interface AddAndDeleteListener {
        void addImag();

        void viewImag(int pos);

        void deletImg(int pos);

    }

    @Override
    public int getCount() {
        int count = mList == null ? 1 : mList.size() + 1;
        if (count >= maxCount) {
            return maxCount;
        }

        return count;
    }

    public void refreshList(List<PicBean> list) {
        this.mList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.item_pic_layout, null);
            viewHolder = new ViewHolder();
            viewHolder.picItemImg = (ImageView) view.findViewById(R.id.picItemImg);
            viewHolder.deleteBtn = (ImageButton) view.findViewById(R.id.deleteBtn);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if ((mList != null && position < mList.size()) || mList.size() == maxCount) {

            // Glide.with(this).load(new File(images.get(i).getCompressPath())).into(imageView1);
            Glide.with(context).load(mList.get(position).getPhotoPath())
                    .into(viewHolder.picItemImg);
           // viewHolder.deleteBtn.setVisibility(View.VISIBLE);
            viewHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.deletImg(position);
                }
            });
            viewHolder.picItemImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.viewImag(position);
                }
            });
        } else {
            viewHolder.deleteBtn.setVisibility(View.GONE);
            Glide.with(context)
                    .load(R.drawable.add_feed_back)
                    .into(viewHolder.picItemImg);
            viewHolder.picItemImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.addImag();
                }
            });
        }


//       if (mList != null && position < mList.size() || mList.size() == maxCount){
//           PicBean picBean = mList.get(position);
//           viewHolder.picItemImg.setImageBitmap(picBean.getBitmap());
//           viewHolder.deleteBtn.setVisibility(View.VISIBLE);
//           Glide.with(context)
//                   .load(picBean.getPhotoPath())
//                   .thumbnail(0.1f)
//                   .into(viewHolder.picItemImg);
//
//        }
//        if(mList.size()==0||position>mList.size()-1){
//            //size=1 , pos =0是图片，pos=1是"+"
//            viewHolder.picItemImg.setImageResource(R.drawable.add_feed_back);
//        }else {
//            PicBean picBean = mList.get(position);
//            viewHolder.picItemImg.setImageBitmap(picBean.getBitmap());
//            viewHolder.deleteBtn.setVisibility(View.VISIBLE);
//            Glide.with(context)
//                    .load(picBean.getPhotoPath())
//                    .thumbnail(0.1f)
//                    .into(viewHolder.picItemImg);
//        }

//        PicBean picBean = mList.get(position);
//        if (picBean.getStatus() == 0) {
//            viewHolder.picItemImg.setImageResource(R.drawable.add_feed_back);
//            viewHolder.deleteBtn.setVisibility(View.GONE);
//        } else {
//            viewHolder.picItemImg.setImageBitmap(picBean.getBitmap());
//            viewHolder.deleteBtn.setVisibility(View.VISIBLE);
//            Glide.with(context)
//                    .load(picBean.getPhotoPath())
//                    .thumbnail(0.1f)
//                    .into(viewHolder.picItemImg);
//
//
//        }
      // viewHolder.deleteBtn.setOnClickListener(new MyOnClick(position));
        return view;
    }

    private static class ViewHolder {
        private ImageView picItemImg;
        private ImageButton deleteBtn;
    }

    public void setAddAndDeleteListener(AddAndDeleteListener listener) {
        this.listener = listener;
    }



//    private class MyOnClick implements View.OnClickListener {
//        private int position;
//
//        public MyOnClick(int position) {
//            this.position = position;
//        }
//
//        @Override
//        public void onClick(View view) {
//            mList.remove(position);
//            notifyDataSetChanged();
//        }
//    }
}
